/**
 * 
 */
package com.everfro.codeshare.views;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;

import com.everfro.codeshare.Activator;

public class ViewContentProvider implements IStructuredContentProvider {
	
	
	private Object[] historyItems = new String[] { "One", "Two", "Three" };
	
	public ViewContentProvider() {
		// TODO Auto-generated constructor stub
		System.out.println("creating the view content provider!");
	
		//Create a folder listner
		
		//call the scan folder action, returns a number of items
		
		scanHistoryFolder();
	
	}
	
	public void inputChanged(Viewer v, Object oldInput, Object newInput) {
	}
	public void dispose() {
		System.out.println("disposing the view content provider");
	}
	public Object[] getElements(Object parent) {
		
		return historyItems;
	}
	
	public void scanHistoryFolder(){
		
		ArrayList preferences = new ArrayList();
		
		String historyPath = Activator.getDefault().getStateLocation().toOSString()+"/history";
		// read a directory

		File rootdir = new File(historyPath);
		
		if(rootdir.isDirectory()){
			File[] files = rootdir.listFiles();
			
			for (int i = 0; i < files.length; i++) {
				File histFile = files[i];
				//open that and create a folderItem I guess
				try {
					if(histFile.isFile()){
					FileInputStream fis = new FileInputStream(histFile);
					Properties prop = new Properties();
					prop.load(fis);
					preferences.add(new HistoryItem(prop, histFile.getAbsoluteFile().getPath()));
					}
					
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
			
			historyItems = preferences.toArray();
			
		}
		
	}
	
	
	
}